package cqgans.app.sunao.com.cqgans.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cqgans.app.sunao.com.cqgans.adapter.PurchaseAdapter;
import cqgans.app.sunao.com.cqgans.bean.Account;
import cqgans.app.sunao.com.cqgans.bean.UserPurchase;
import cqgans.app.sunao.com.cqgans.common.AppConstant;
import cqgans.app.sunao.com.cqgansnew.R;

import static cqgans.app.sunao.com.cqgans.common.AppConstant.user;

/**
 * Created by sunao on 2017/6/12.
 */
@SuppressWarnings("WrongConstant")
@ContentView(R.layout.activity_oldhome)
public class OldHomeActivity extends BaseActivity implements SearchView.OnQueryTextListener {
    @ViewInject(R.id.searchView)
    private SearchView searchView;

    @ViewInject(R.id.home_loading)
    private RelativeLayout home_loading;
    @ViewInject(R.id.home_info_ll)
    private LinearLayout home_info_ll;
    @ViewInject(R.id.account_no)
    private TextView account_no;
    @ViewInject(R.id.account_name)
    private TextView account_name;
    @ViewInject(R.id.account_addr)
    private TextView account_addr;
    @ViewInject(R.id.account_buys)
    private TextView account_buys;
    @ViewInject(R.id.home_button)
    private Button home_button;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    @ViewInject(R.id.home_noinfo)
    private TextView home_noinfo;
    @ViewInject(R.id.change_pwd)
    private TextView change_pwd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTitleBar("安燃通", 3);
        sharedPreferences = getSharedPreferences("cqgas", Context.MODE_PRIVATE);

        initLayout();

    }

    private void initLayout() {
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);
        home_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(OldHomeActivity.this, ProductActivity.class));
            }
        });
        TextView close = (TextView) this.findViewById(R.id.close);
        close.setVisibility(View.VISIBLE);
        close.setText("切换账号");
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppConstant.user = null;
                editor = sharedPreferences.edit();//获取编辑器
                editor.putInt("id", 0);
                editor.commit();
                startActivity(new Intent(OldHomeActivity.this, MainActivity.class));
                finish();
            }
        });
        findViewById(R.id.muilt_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(OldHomeActivity.this, HomeActivity.class));
            }
        });
        findViewById(R.id.user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppConstant.TARGET_URL = AppConstant.JSS_ACCOUNT_URL;
                startActivity(new Intent(OldHomeActivity.this, WebActivity.class));
            }
        });
        findViewById(R.id.user_ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppConstant.TARGET_URL = AppConstant.JSS_ACCOUNT_URL;
                startActivity(new Intent(OldHomeActivity.this, WebActivity.class));

            }
        });
        change_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(OldHomeActivity.this, ChangePwdActivity.class));

            }
        });

        findViewById(R.id.enterprise_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppConstant.account = null;
                Intent i = new Intent(OldHomeActivity.this, ProductActivity.class);
                i.putExtra("userType", 1);
                startActivity(i);
            }
        });

        findViewById(R.id.personal_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppConstant.account = null;
                Intent i = new Intent(OldHomeActivity.this, ProductActivity.class);
                i.putExtra("userType", 2);
                startActivity(i);
            }
        });
    }

    @Override
    public boolean onQueryTextSubmit(String s) {
        if (!TextUtils.isEmpty(s)) {
            search(s);
        }
        return true;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        return false;
    }

    private void search(String s) {
        RequestParams params = new RequestParams(AppConstant.URL + AppConstant.QUERY_BY_CODE_URL);
//        params.addQueryStringParameter("userCode",s);
//        params.addQueryStringParameter("staffMobile",AppConstant.user.getMobile());
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        LogUtil.i("imm.isActive() = "+imm.isActive());
        if (imm.isActive()) {
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//开启或者关闭软键盘
        }
        home_loading.setVisibility(View.VISIBLE);

        JSONObject mjson = new JSONObject();

        try {
            mjson.put("userCode", s);
            mjson.put("staffMobile", user.getMobile());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        params.setAsJsonContent(true);
        params.setBodyContent(mjson.toString());
        params.addHeader("Content-Type", "application/json");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @SuppressWarnings("WrongConstant")
            @Override
            public void onSuccess(String result) {
                LogUtil.i(result);
                System.out.println(result);
                home_loading.setVisibility(View.GONE);
                try {
                    JSONObject resultJson = new JSONObject(result);
                    LogUtil.i(resultJson.toString());

                    if (resultJson.has("flag") && resultJson.getBoolean("flag")) {
                        Gson gson = new Gson();
                        if (resultJson.has("data")) {
                            JSONObject data = resultJson.getJSONObject("data");
                            JSONObject entity = data.getJSONObject("entity");
                            Account a = gson.fromJson(entity.toString(), Account.class);
                            AppConstant.account = new UserPurchase();
                            AppConstant.account.userName = a.getYQDZ_LXR();
                            AppConstant.account.userType = a.getYddzkhLx().equals("个人用户") ? 2 : 1;
                            AppConstant.account.userAddress = a.getYQDZ_SM();
                            AppConstant.account.userName = a.getYQDZ_YQZH();
                            home_info_ll.setVisibility(View.VISIBLE);
                            home_noinfo.setVisibility(View.GONE);
                            account_no.setText(a.getYQDZ_YQZH());
                            account_name.setText(AppConstant.account.userType == 1? PurchaseAdapter.encodeAddr(a.getYQDZ_LXR()) : PurchaseAdapter.encodeName(a.getYQDZ_LXR()));
                            account_addr.setText(PurchaseAdapter.encodeAddr(a.getYQDZ_SM()));
                            account_buys.setText(a.getBuyStatus());

                        }
                    } else {
                        home_info_ll.setVisibility(View.INVISIBLE);
                        home_noinfo.setVisibility(View.VISIBLE);

                        Toast.makeText(OldHomeActivity.this, "您输入的燃气账号有误，请重新输入", 1000).show();
                    }

                } catch (JSONException e) {
                    home_noinfo.setVisibility(View.VISIBLE);

                    home_info_ll.setVisibility(View.INVISIBLE);

                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                home_loading.setVisibility(View.GONE);
                LogUtil.e(ex.getMessage());
                Toast.makeText(OldHomeActivity.this, ex.getMessage(), 1000).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {
                home_loading.setVisibility(View.GONE);

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
